<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../../js/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/common.css" media="all">
    <script src="../../js/layui/layui.all.js"></script>
    <script src="../../js/token.js"></script>
    <script src="../../js/config.js"></script>
    <style>
        .layui-fluid{
            height: auto;
        }
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div style="text-align: center;">
                <div class="layui-inline">
                    <input class="layui-input" name="id" id="test-table-demoReload" placeholder="输入昵称或账号" autocomplete="off">
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin search" lay-submit lay-filter="LAY-user-back-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>搜索
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table class="layui-hide" id="t" lay-filter="t"></table>
        </div>
    </div>
</div>
</body>

<script type="text/html" id="tb">
    <div class="layui-container" style="margin-left:0px;">
        <a class="layui-btn layui-btn-xs" lay-event="add"><i class="layui-icon layui-icon-addition"></i>新增管理员</a>
    </div>
</script>

<script type="text/html" id="caozuo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">角色编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
    <!--<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="chongzhi">重置密码</a>-->
</script>

<script>
    var $ = layui.$
    var table = layui.table
    table.render({
        elem: '#t',
        id: 'tt',
        toolbar: '#tb',
        url: HOST+'admin/role/adminRole',
        cols: [
            [{
                field: 'id',
                title: '序号',
                width: '13%',
                type:'numbers',
                align: 'center'
            },
                {
                    field: 'username',
                    title: '管理员账号',
                    width: '20%',
                    align: 'center'
                },

                {
                    field: 'role_name',
                    title: '角色',
                    width: '30%',
                    align: 'center'
                },
                {
                    title: '操作',
                    width: '17%',
                    align: 'center',
                    toolbar: '#caozuo',
                    fixed: 'right'
                }
            ]
        ],
        limit:40,
        page: true
    });

    //监听工具条
    table.on('tool(t)', function (obj) {
        var data = obj.data
        if (obj.event === 'detail') {
            var index = layer.open({
                type: 2,
                area: ['100%', '100%'],
                content: 'editrole.html?adminId='+data.id, //这里content是一个普通的String
                end: function(){
                    location.reload();
                }
            });
            layer.full(index);
        }else if (obj.event === 'delete') {
            layer.confirm("确定删除用户吗？",{icon: 3, title:'提示'},function (index) {
                $.ajax({
                    type:"get",
                    url:HOST+"user/deleteUser?id=" + data.id,
                    success:function (res) {
                        if(res.code=200){
                            layer.msg(res.msg);
                            layer.close(index);
                            table.reload("tt");
                        }else if (res.code == 500) {
                            layer.msg(res.msg);
                        }else {
                            layer.msg("拒绝访问");
                        }
                    }
                })
            })
        }else if(obj.event === 'chongzhi'){
            layer.confirm("确定重置密码吗?",{icon: 3, title:'提示'},function (index) {
                $.ajax({
                    type:"post",
                    url:HOST+"user/resetPassword",
                    data:{
                        id:data.id
                    },
                    dataType:"json",
                    success:function (res) {
                        if(res.code=200){
                            layer.msg(res.msg);
                        }else{
                            layer.msg("操作失败");
                        }
                    }
                })
            })
        }
    });

    table.on('toolbar(t)', function (obj) {
        var data = obj.data;
        // console.log(data);
        if (obj.event === 'add') {
            var index = layer.open({
                type: 2,
                area: ['100%', '100%'],
                content: 'add_user.html', //这里content是一个普通的String
                end: function(){
                    location.reload();
                }
            });
            layer.full(index);
        }
    });

    $('.test-table-operate-btn .layui-btn').on('click', function () {
        var type = $(this).data('type')
        active[type] ? active[type].call(this) : ''
    });

    $('.search').on('click',function () {
        var text = $('#test-table-demoReload').val();
        table.reload('tt',{
            url:HOST+'admin/role/adminRole',
            where:{
              text:text
            },
            page:{
                curr:1
            }
        })
    })
</script>
</html>
